﻿//===============================================================================
// Apolineo Patterns & Practices LeanEngine Framework
//===============================================================================
// Copyright © Apolineo Desenvolvimento de Sistemas Ltda.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LeanEngine.Entity;
using LeanEngine.Extensions;

namespace LeanEngine.SalesConcept.Windows.Component
{
    public partial class ControlOfItemSelection : UserControl
    {
        #region Declarations Members

        [Category("Property Changed"),
        Browsable(true)]
        public event Action<ControlOfItemSelection, PlainEntity> SelectedItemChanged;

        #endregion

        #region Constructor Members

        public ControlOfItemSelection()
        {
            InitializeComponent();
        }

        #endregion

        #region Property Members

        [Browsable(false)]
        public FormSelectItemBase FormSelect { get; set; }

        private PlainEntity _selectedValue;
        [Browsable(false)]
        public PlainEntity SelectedValue
        {
            get { return this._selectedValue; }
            set
            {
                this._selectedValue = value;
                if (value.IsNotNull())
                {
                    if (this.SelectedItemChanged.IsNotNull()) this.SelectedItemChanged.Invoke(this, value);
                    this.txtItemText.Text = value.ToString();
                }
                else
                {
                    this.txtItemText.Text = string.Empty;
                }
            }
        }

        [Browsable(true)]
        [Category("Appearance")]
        [Description("The text of the label.")]
        public override string Text
        {
            get { return this.lblText.Text; }
            set { this.lblText.Text = value; }
        }

        #endregion

        #region Event Members

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (this.FormSelect.IsNull()) throw new InvalidOperationException("The FormSelect property cannot be null.");

            this.FormSelect.StartPosition = FormStartPosition.CenterScreen;
            this.SelectedValue = this.FormSelect.ShowDialogForSelectItem();
        }

        private void txtItemText_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSelect_Click(sender, e);
            }
        }

        #endregion
    }
}
